1766C - Hamiltonian Wall - CodeForces Solution


dp implementation *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","O3")
#endif // ONLINE_JUDGE

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define rnk(x, y) upper_bound(all((x)), (y)) - (x).begin()

template <class T = int>
using ii = pair<T, T>;
template <class T = int>
using tri = tuple<T, T, T>;

typedef long double ld;
typedef long long ll;
// typedef __int128 LL;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

static int rnd(int lo, int hi)
{
    return uniform_int_distribution<int>(lo, hi)(rng);
}

int Get(int k, vector<int> &p)
{
    return p[k] == k ? k : p[k] = Get(p[k], p);
}

typedef vector<vector<int>> mat;

// global variables

const ld eps = 1e-9;
const ll oo = 2e18 + 100;

const int MAX = 2e5+10;
const ll mod = 1e9+7;

int iterate(vector<string> &s, int posx, int posy)
{
    vector<vector<bool>> band(2, vector<bool> (s[0].size()));
    band[posy][posx] = true;
    int cont = 1;
    while(true)
    {
        if(s[(posy+1)%2][posx] == 'B' && !band[(posy+1)%2][posx]){
            posy = (posy+1)%2;
            band[posy][posx] = true;
            cont++;
            continue;   
        }
        if(posx<s[0].size()&& !band[posy][posx+1] && s[posy][posx+1] == 'B'){
            posx = posx+1;
            band[posy][posx] = true;
            cont++;
            continue;
        }
        return cont;
    }
}

void solve()
{
    int n; cin >> n;
    vector<string> s(2);
    int cont = 0;
    for(int i=0;i<2;i++){
        cin >> s[i];
        for(int j=0;j<n;j++){
            if(s[i][j] == 'B'){
                cont++;
            }
        }
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        if(s[0][i] == 'B'){
            ans = max(ans, iterate(s, i, 0));
        }
        if(s[1][i] == 'B'){
            ans = max(ans, iterate(s, i, 1));
        }
        if(s[0][i] == 'B' || s[1][i] == 'B'){
            break;
        }
    }
    if(ans == cont) {
        cout << "YES\n";
        return;
    }
    cout << "NO\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE

    int t = 1; cin >> t;
    for (int i = 1; i <= t; i++){
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins